﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDSP.BRL;
using BLL.Util;
using System.IO;
using System.Web;

namespace LDSP.Util.Mail
{
    public class CodificationMailInfo : MailInfo
    {
        private IList<Codification> _selectedCodification;
        private IList<Codification> SelectedCodification
        {
            get
            {
                if (_selectedCodification == null)
                {
                    _selectedCodification = new List<Codification>();

                    string idsStr = SelectedIds;
                    if (!string.IsNullOrEmpty(idsStr))
                    {
                        string[] ids = idsStr.Split('a');
                        for (int i = 0; i < ids.Length; i++)
                        {
                            int id = 0;
                            if (Int32.TryParse(ids[i], out id))
                            {
                                Codification ett = new Codification();
                                ett.GetProperties(id);
                                _selectedCodification.Add(ett);
                            }
                        }
                    }
                }

                return _selectedCodification;
            }
        }

        public CodificationMailInfo(string selectedIds)
            : base(selectedIds)
        {
            Subject = "法律法规服务平台-最近新增法规一览";
        }

        public override void InitMailContent()
        {
            string content = string.Empty;
            string attachList = string.Empty;
            if (SelectedCodification.Count > 0)
            {
                foreach (Codification cod in SelectedCodification)
                {
                    content += cod.Name + "\n";

                    //若有附件，则将法规名作为邮件发送的附件名。
                    if (!string.IsNullOrEmpty(cod.FileName))
                    {
                        attachList += MailSender.CorrectChineseName(cod.Name) + ".pdf;";
                    }
                }

                content += string.Format(Constants.Info.Mail_Postfix, DateTime.Now.ToShortDateString());
            }

            Subject = "法律法规服务平台-最近新增法规一览";
            Attachments = attachList;
            Content = content;
        }

        public override IList<string> GetAttachmentList(HttpServerUtility server)
        {
            IList<string> fileList = new List<string>();
            if (SelectedCodification.Count > 0)
            {
                foreach (Codification cod in SelectedCodification)
                {
                    //拷贝文件到临时目录。
                    string source = server.MapPath("~\\Docs\\" + cod.FileExtension);

                    if (File.Exists(source))
                    {//若有法规附件，则添加到邮件附件。
                        string destination = server.MapPath("~\\TmpImg\\" + cod.Name + ".pdf");

                        if (!File.Exists(destination))
                        {
                            File.Copy(source, destination);
                        }

                        fileList.Add(destination);
                    }
                    else if (!string.IsNullOrEmpty(cod.FileName))
                    {//若无附件，但系统数据里有文件名，则添加空字串进附件文件列表。
                        fileList.Add("");
                    }
                }
            }

            return fileList;
        }
    }
}
